package com.cn.dao;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


import com.cn.entity.News;

public class NewsDao {
	public static SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	Session session;
	
	 public void addNews(News news){
		   
		   session=sessionFactory.openSession();
		   Transaction tr=session.beginTransaction();
		   session.save(news);
		   tr.commit();
		   session.close();
	 }
	 public List<News> selectNews(){
		 
		 session=sessionFactory.openSession();
		 
		 String hql="from News";
		 
		 Query query=session.createQuery(hql);
		 
		 List<News> list=query.list();		 
		 return list;
	 }
	 
	 
 public News selectNews(int newsid){
		 News news=null;
		 session=sessionFactory.openSession();
		 
		 String hql="from News where newsid=?";
		 
		 Query query=session.createQuery(hql);
		 query.setInteger(0, newsid);
		 List<News> list=query.list();	
		 news=list.get(0);
		 
		 return news;
	 }
	 
	 
	 
	 
	 


}
